package web;

import com.google.gson.Gson;
import pojo.User;
import service.UserServcive;
import service.impl.UserServiceImpl;
import utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY;

public class UserServlet extends BaseServlet {
    private UserServcive user = new UserServiceImpl();

    public void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        //一次性把所有请求的参数注入到javaBean对象中
        Map<String, String[]> parameterMap = req.getParameterMap();
        User user1 = WebUtils.getParam(parameterMap, new User());
        System.out.println(user1);
        if (user.login(user1) == null) {
            req.setAttribute("msg", "用户不存在或密码错误");
            req.setAttribute("username", username);
            req.getRequestDispatcher("/pages/user/login.jsp").forward(req, resp);
        } else {
            User login = user.login(user1);
            req.getSession().setAttribute("user", login);
            System.out.println(login);
            System.out.println("登录成功");
            req.getRequestDispatcher("/pages/user/login_success.jsp").forward(req, resp);
        }
    }

    public void regist(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String attribute = (String) req.getSession().getAttribute(KAPTCHA_SESSION_KEY);
        req.getSession().removeAttribute(attribute);
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String repasw = req.getParameter("repwd");
        String email = req.getParameter("email");
        String code = req.getParameter("code");
        //一次性把所有请求的参数注入到javaBean对象中
        Map<String, String[]> parameterMap = req.getParameterMap();
        User user1 = WebUtils.getParam(parameterMap, new User());
        System.out.println(user1);
        if (attribute.equalsIgnoreCase(code) && attribute != null) {
            if (repasw.equals(password)) {
                user.register(user1);
                System.out.println("注册成功！！");
                req.getRequestDispatcher("/pages/user/regist_success.jsp").forward(req, resp);
            } else {
                req.getRequestDispatcher("/pages/user/regist.jsp").forward(req, resp);
            }
        } else {
            req.setAttribute("username", username);
            req.setAttribute("password", password);
            req.setAttribute("repwd ", repasw);
            req.setAttribute("email", email);
            req.setAttribute("msg", "验证码错误");
            req.getRequestDispatcher("/pages/user/regist.jsp").forward(req, resp);
        }
    }

    public void logout(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getSession().invalidate();
        resp.sendRedirect(req.getContextPath() + "/index.jsp");
    }

    public void ajaxExist(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        boolean isFlag = user.existUsername(username);
        Map<String,Object> map = new HashMap<>();
        map.put("isFlag",isFlag);
        Gson gson = new Gson();
        String s = gson.toJson(map);
        resp.getWriter().write(s);
    }
}